if(typeof String.prototype.trim !== 'function') {
	  String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g, '');
	  }
}

function openOrderDetail(orderID){	
	faceboxClose();
	//window.open('order.do?action=orderDetail&id='+orderID,orderID);
	location.href='order.do?action=orderDetail&id='+orderID;
}
function cancelOrder(orderId,pg){
	if(!window.confirm('您确认要执行此操作吗?')){
		return ;
	}
	var remark = prompt("备注");
	$.ajax({
		url:'ajaxServlet?action=cancelOrder&orderId='+orderId,
		method:'post',
		data:{'remark':remark},
		success:function(res){
			if('Fail' != res){
				//page(pg);
				
				openOrderDetail(orderId);
			}else{
				alert('取消订单失败!');
			}
		}
	});
}

var TMP_DATA=[];
function cancelResetT(obj,orderId){
	var divObj=$(obj).parent().parent();
	divObj.html(TMP_DATA['T'+orderId]);
}
function cancelResetE(obj,orderId){
	var divObj=$(obj).parent().parent();
	divObj.html(TMP_DATA['E'+orderId]);
}
function confirmT(obj,orderId,pg){
	var divObj=$(obj).parent().parent();
	var oldHtml=TMP_DATA['T'+orderId];
	var accountId=$('#updateTicket'+orderId).val();
	var checkText=$('#updateTicket'+orderId).find("option:selected").text();
	var textOld=$('<div>'+oldHtml+'</div>').find('div[queryKey=ticketAccount]').text();
	if(checkText.trim() != textOld.trim()){
		$.ajax({
			url:'ajaxServlet?action=updateTicketAccount',
			method:'post',
			data:{orderId:orderId,accountId:accountId},
			success:function(res){
				if('Fail' != res){
					//page(pg);
					
					openOrderDetail(orderId);
				}else{
					alert('更新帐号失败!');
				}
			}
		});
	}else{
		divObj.html(oldHtml);
		divObj.find('div[queryKey=ticketAccount]').text(checkText);
	}
}
function confirmE(obj,orderId,pg){
	var divObj=$(obj).parent().parent();
	var oldHtml=TMP_DATA['E'+orderId];
	var accountId=$('#updateExpress'+orderId).val();
	var checkText=$('#updateExpress'+orderId).find("option:selected").text();
	var textOld=$('<div>'+oldHtml+'</div>').find('div[queryKey=expressAccount]').text();
	if(checkText.trim() != textOld.trim()){
		$.ajax({
			url:'ajaxServlet?action=updateExpressAccount',
			method:'post',
			data:{orderId:orderId,accountId:accountId},
			success:function(res){
				if('Fail' != res){
					//page(pg);
					
					openOrderDetail(orderId);
				}else{
					alert('更新帐号失败!');
				}
			}
		});
	}else{
		divObj.html(oldHtml);
		divObj.find('div[queryKey=expressAccount]').text(checkText);
	}
}
function resetTaUser(orderId,obj,src,pg){
	var divObj=$(obj).parent().parent();
	TMP_DATA['T'+orderId]=divObj.html();
	var oldText=divObj.find('div[queryKey=ticketAccount]').text();
	$.ajax({
		url:'ajaxServlet?action=getTicketAccount&city='+encodeURI(src),
		method:'post',
		dataType:'jsonp',
		jsonp:'callback',
		success:function(res){
			var htmlStr="<div><select id='updateTicket"+orderId+"'>";
			if(res.length > 0){
				$.each(res,function(item,obj){
					var accountId=obj.accountId;
					var username=obj.username;
					var selected;
					if(oldText.trim() == username.trim()){
						selected="selected";
					}else{
						selected="";
					}
					htmlStr+="<option value='"+accountId+"' "+selected+">"+username+"</option>";
				});
			}
			htmlStr+="</select></div>";
			htmlStr+="<div style='padding-top:5px;'><a href='javascript:void(0);' onclick='confirmT(this,\""+orderId+"\",\""+pg+"\");'>确定</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='cancelResetT(this,\""+orderId+"\");'>取消</a></div>";
			divObj.html(htmlStr);
		}
	});	
}

function resetEaUser(orderId,obj,src,pg){
	var divObj=$(obj).parent().parent();	
	TMP_DATA['E'+orderId]=divObj.html();
	var oldText=divObj.find('div[queryKey=expressAccount]').text();
	$.ajax({
		url:'ajaxServlet?action=getExpressAccount&city='+encodeURI(src),
		method:'post',
		dataType:'jsonp',
		jsonp:'callback',
		success:function(res){
			var htmlStr="<div><select id='updateExpress"+orderId+"'>";
			if(res.length > 0){
				$.each(res,function(item,obj){
					var accountId=obj.accountId;
					var username=obj.username;
					var selected;
					if(oldText.trim() == username.trim()){
						selected="selected";
					}else{
						selected="";
					}
					htmlStr+="<option value='"+accountId+"' "+selected+">"+username+"</option>";
				});
			}
			htmlStr+="</select></div>";
			htmlStr+="<div style='padding-top:5px;'><a href='javascript:void(0);' onclick='confirmE(this,\""+orderId+"\",\""+pg+"\");'>确定</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='cancelResetE(this,\""+orderId+"\");'>取消</a></div>";
			divObj.html(htmlStr);
		}		
	});	
}

function updateStauts(status,orderId,pg){
	if(!window.confirm('您确认要执行此操作吗?')){
		return ;
	}
	var url;
	if(status == 0){
		url='ajaxServlet?action=sureOrder&orderId='+orderId;
	}else if(status == 1){
		url='ajaxServlet?action=payOrder&orderId='+orderId;
	}else if(status == 2){
		/*
		var $externalOrder=prompt('备注你的外部订单号');
		if(!$externalOrder || $externalOrder == ''){
			return ;
		}
		*/
		url='ajaxServlet?action=ticketedOrder&orderId='+orderId;//+"&externalOrder="+$externalOrder;
	}else if(status == 3){
		url='ajaxServlet?action=notTicketOrder&orderId='+orderId;
	}else if(status == 4){
		var $expressID=prompt('请输入您配送的订单号','0');
		url='ajaxServlet?action=expressedOrder&orderId='+orderId+'&expressID='+$expressID;
	}else if(status == 5){
		url='ajaxServlet?action=receivedOrder&orderId='+orderId;
	}else if(status == 6){
		url='ajaxServlet?action=notExpressOrder&orderId='+orderId;
	}else if(status == 7){
		url='ajaxServlet?action=completedOrder&orderId='+orderId;
	}else if(status == 8){
		url='ajaxServlet?action=refundOrder&orderId='+orderId;
	}else if(status == 997 || status == 996){
		url='ajaxServlet?action=refundCha&orderId='+orderId+'&status='+status;
	}else if(status == 999){
		url='ajaxServlet?action=cancelOrder&orderId='+orderId+'&status='+status;		
	}
	if(url && url != ''){
		$.ajax({
			url:url,
			method:'post',
			success:function(res){
				if('Fail' != res){
					//$('#id').val(orderId);
					//$('#status').val('');
					//page(pg);					
					openOrderDetail(orderId);
				}else{
					alert('更新订单失败!');
				}
			}
		});
	}
}

function getPassenger(orderId){	
	var obj=$('#detail'+orderId);
	//var isActionObj=obj.find('input[name=isAction]');
	var dispaly=obj.css('display');
	//var isAction=isActionObj.val();
	if(dispaly == 'none'){
		//if(isAction == 'false'){
			$.ajax({
				url:'ajaxServlet?action=getPassenger&orderId='+orderId,
				method:'post',
				dataType:'jsonp',
				jsonp:'callback',
				success:function(res){
					if(res && res.length > 0){					
						var i=0;
						var queryString='tr[id*=passenger'+orderId+']';
						$(queryString).each(function(){
							if(i!=0){
								$(this).remove();
							}
							i++;
						});
						i=0;
						var passengerObj=$('#passenger'+orderId);					
						$.each(res,function(item,obj){
							if(i>0){
								var tmpPg=passengerObj;
								passengerObj=passengerObj.clone();
								tmpPg.after(passengerObj);
							}
							passengerObj.find('span[id=index]').text(i+1);
							passengerObj.find('td[id=name]').text(obj.name);
							passengerObj.find('td[id=card_type]').text(obj.cardTypeString);
							passengerObj.find('td[id=card_id]').text(obj.cardId);
							i++;
						});
					}
					//isActionObj.val('true');					
				}
			});
		//}
		obj.show();
	}else{
		obj.hide();
	}
}


function updateActive(id,parentAccount,isActive){
	if(isActive == 0 ){
		if(!window.confirm('暂停帐号将使所有的子帐号也暂停,您确定执行此操作吗?')){
			return false;
		}
	}else{
		if(!window.confirm('您将启用帐号,您确定执行此操作吗?')){
			return false;
		}
	}
	$.ajax({
		url:'ajaxServlet?action=updateActive',
		method:'post',
		data:{'id':id,'parentAccount':parentAccount,'isActive':isActive},
		success:function(res){
			if('Fail'!=res){
				if(0 == isActive){
					$('#status'+id).html('<span>暂停&nbsp;&nbsp;<a href="javascript:void(0)" onclick="updateActive(\''+id+'\',\''+parentAccount+'\',1);">启用</a></span>');
				}else if(1 == isActive){
					$('#status'+id).html('<span>启用&nbsp;&nbsp;<a href="javascript:void(0)" onclick="updateActive(\''+id+'\',\''+parentAccount+'\',0);">暂停</a></span>');
				}
				$('#lastUpdate'+id).html(res);
			}else{
				alert('更新用户状态失败!');
			}
		}
	});
}

function modify(){
	$('#dataTable').hide();
	$('#hiddenTable').show();
	
}
function cannel(){
	$('#hiddenTable').hide();
	$('#dataTable').show();
}

function getCities(city,district){	
	var province=$('#province').val();
	$.ajax({
		url:_PRO_NAME+'/ajaxServlet',
		method:'post',
		data:{'action':'getCities','province':province},
		success:function(res){
			$('#city').html($(res).html());
			$('#district').html('');
			if(city){
				$('#city').val(city);getDistricts(district);
			}else{
				getDistricts();
			}		
		}
	})
}

function getDistricts(district){
	var province=$('#province').val();
	var city=$('#city').val();
	$.ajax({
		url:_PRO_NAME+'/ajaxServlet',
		method:'post',
		data:{'action':'getDistricts','province':province,'city':city},
		success:function(res){
			$('#district').html($(res).html());
			if(district){
				$('#district').val(district);
			}
		}
	})
}

function facebox(obj){
	obj.facebox({
		classname     : 'facebox',
	    loading_image : 'loading.gif',
	    close_image   : 'closelabel.gif',
		dimwholepage  : true,
		dimclick      : true,
		faceboxHtml   : /*$('#faceboxHTML').html()*/
			'<div id="facebox" style="display:none;" class="txt_dark_s">'+
				'<div class="popup">'+
					'<table id="facebox_moretable" style="width:500px;">'+
						'<tbody>'+
							'<tr> '+
								'<td class="tl"/>'+                    
								'<td class="b"/>'+                          
								'<td class="tr"/>'+
							'</tr>'+
							'<tr>'+
								'<td class="b"/>'+                   
								'<td class="body">'+
								 '<div align="center">'+
								 '<div style="float:right; padding-top:8px; padding-right:3px;"><a href="javascript:void(0)" class="close"><img src="closelabel.gif" title="close" class="close_image" /></a></div>'+
								 '<div style="clear:both;"></div>'+
								 '<div class="content" id="faceboxContent" style="padding:5px 3px 0 10px;"></div>'+
								 '</div>'+	
								'</td>'+
								'<td class="b"/>'+        
							'</tr>'+
							'<tr>'+
								'<td class="bl"/>'+                      
								'<td class="b"/>'+
								'<td class="br"/>'+                          
							'</tr>'+
						'</tbody>'+
					'</table>'+
				'</div>'+
			'</div>'
	});
}

function updateRemark(pg){	
	var $id			= $('#remarkID').val();
	var $htRemark	= $('#htRemark').val();
	if($id && $htRemark){
		$('#facebox_moretable .content').html('<img src="scripts/js/jquery/facebox/loading.gif" alt="loading" />');
		$.ajax({
			url:_PRO_NAME+'/order.do',
			method:'post',
			data:{'action':'updateRemark','id':$id,'htRemark':$htRemark},
			success:function(res){
				if(res && 'true' == res){
					//$('#id').val($id);
					//$('#status').val('');
					//page(pg);					
					openOrderDetail($id);
				}					
			}
		});
	}else{
		alert('参数错误，请检查!');
	}
}
function fillFaceboxContent(html){
	$('#facebox_moretable .content').html(html);
}
function sendSms(){
	var $orderID= $('#orderID').val();
	var $phone	= $('#phone').val();
	var $content= $('#content').val();
	var $type	= $('#type').val();
	$content	= encodeURIComponent($content);
	var $old	= $('#facebox_moretable .content').html();
	if($orderID && $phone && $content){
		fillFaceboxContent('<img src="scripts/js/jquery/facebox/loading.gif" alt="loading" />');
		$.ajax({
			url		:'ajaxServlet?action=sendSms',
			method	: 'Post',
			async	: true,
			data	:{'orderID':$orderID,'phone':$phone,'content':$content,'type':$type},
			success	:function(res){
				var $res	= $(res).text();
				if($res == 'Success'){
					faceboxClose();
					alert('发送成功!');					
				}else{
					fillFaceboxContent($old);
					alert($res);
				}
			}
		});
	}else{
		alert('参数错误，请检查!');
	}
}
function faceboxClose(){
	$.facebox.close();
}